package com.tian.common.util;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2025-03-17 10:08
 * {@code @version:} 1.0
 */
public class DistanceCalculateUtil {

    public static void main(String[] args) {
        System.out.println(getDistance(113.254576,23.115149,113.47848100,23.10031600));
    }
    //计算
    public static double getDistance(Double ALongitude, Double ALatitude, Double BLongitude, Double BLatitude) {

        double firstRadLat = rad(ALatitude);
        double firstRadLng = rad(ALongitude);
        double secondRadLat = rad(BLatitude);
        double secondRadLng = rad(BLongitude);

        double a = firstRadLat > secondRadLat ? firstRadLat - secondRadLat : secondRadLat - firstRadLat;
        double b = firstRadLng > secondRadLng ? firstRadLng - secondRadLng : secondRadLng - firstRadLng;
        double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(firstRadLat)
                * Math.cos(secondRadLat) * Math.pow(Math.sin(b / 2), 2))) * EarthRadius;
        return Math.round(cal * 10000d) / 10000d;
    }
    /**
     * 地球半径
     */
    private static final double EarthRadius = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
}
